package cn.lyjuan.payment.third.base.pay.chl.impl.iap;


import cn.lyjuan.base.util.JsonUtils;
import cn.lyjuan.payment.third.base.pay.chl.impl.AbstractPayChl;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.bean.*;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.enums.IosDevelopEnum;
import cn.lyjuan.payment.third.base.pay.chl.impl.iap.util.IapSubmit;
import cn.lyjuan.payment.third.base.pay.enums.SimpleChlEnum;
import cn.lyjuan.payment.third.base.pay.enums.SimpleStatusEnum;
import cn.lyjuan.payment.third.base.pay.vo.req.NotifyReq;
import cn.lyjuan.payment.third.base.pay.vo.req.PayReq;
import cn.lyjuan.payment.third.base.pay.vo.req.QueryReq;
import cn.lyjuan.payment.third.base.pay.vo.req.TransferReq;
import cn.lyjuan.payment.third.base.pay.vo.res.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 苹果IAP应用内支付
 * Created by chad on 2016/2/23.
 */
@Slf4j
@Component
public class AppleIapChlImpl extends AbstractPayChl<IapChlCnf, IapPayReq, IapNotifyReq, IapQueryReq, TransferReq.ITransferReq, LatestReceiptInfoVo> {

    @Override
    public SimpleChlEnum chl() {
        return SimpleChlEnum.IAP;
    }

    @Override
    public BaseRes checkPayParams(IapPayReq params) {
        return IapSubmit.checkPayReq(params);
    }

    @Override
    public BaseRes checkTransferParams(TransferReq.ITransferReq params) {
        return new BaseRes(false, "not support");
    }

    @Override
    public IapQueryReq parseQueryParams(Map<String, String> params) {
        return new IapQueryReq(params.get("receipt"), params.get("transactionId"), params.get("productId"), params.get("devType").equals(IosDevelopEnum.Sandbox.getValue()));
    }

    @Override
    public IapNotifyReq parseNotifyParams(HttpServletRequest req) {
        return null;
    }

    @Override
    public PayRes<LatestReceiptInfoVo> order(PayReq<IapPayReq> req, IapChlCnf cnf) {
        return new PayRes<>(true, "iap http verify-request response success", null);
    }

    @Override
    public TransferRes<LatestReceiptInfoVo> transfer(TransferReq<TransferReq.ITransferReq> req, IapChlCnf cnf) {
        return null;
    }

    @Override
    public NotifyRes notifyStatus(NotifyReq<IapNotifyReq> req, IapChlCnf cnf) {
        return null;
    }

    @Override
    public RefundRes orderRefund(String selfRefundNo, String localNo, Integer totalAmt, Integer amt, IapChlCnf cnf) {
        return null;
    }

    @Override
    public QueryRes<IapQueryRes> queryStatus(QueryReq<IapQueryReq> req, IapChlCnf cnf) {
        // 验证 并且 拿到 最新一条数据
        LatestReceiptInfoVo data = null;
        try {
            data = IapSubmit.verify(req.getParams().getReceipt(), cnf.getKey(), req.getParams().getProductId(),
                    req.getParams().getTransactionId(), req.getParams().getDevType()
            );
        } catch (Exception e) {
            log.error("AppleIapChlImpl queryStatus param: ["+JsonUtils.to(req)+"]", e);
            throw new RuntimeException(e);
        }

        // check response
        if (data == null) {
            log.error("AppleIapChlImpl queryStatus error: param: [{}]", JsonUtils.to(req));
            return new QueryRes<>(SimpleStatusEnum.FAIL, "not found iap this transactionId and productId.", req.getParams().getTransactionId(), null);
        }
        IapQueryRes res = new IapQueryRes(data);
        return new QueryRes<>(SimpleStatusEnum.SUCC, "success", req.getParams().getTransactionId(), res);
    }
}
